package com.zb.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Assert;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zb.entity.SelectAddressVo;
import com.zb.entity.ZjWorkUser;
import com.zb.mapper.ZjWorkUserMapper;
import com.zb.service.ZjWorkUserService;
import com.zb.vo.OrderUserVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 工单用户表 服务实现类
 * </p>
 *
 * @author WEN
 * @since 2023-09-20
 */
@Service
public class ZjWorkUserServiceImpl extends ServiceImpl<ZjWorkUserMapper, ZjWorkUser> implements ZjWorkUserService {
    @Resource
    private ZjWorkUserMapper workUserMapper;

    // 保存工单用户
    @Override
    public void save(Long custAccId, Long workId, OrderUserVo orderUser) {
        ZjWorkUser user = new ZjWorkUser();
        BeanUtils.copyProperties(orderUser, user);
        user.setId(workId);
        if (null != this.getById(workId)) {
            this.updateById(user);
            return;
        }
        user.setCustAccId(custAccId);
        user.setHelpOtherCreateOrder(false);
        user.setCreateOrderUserId(0L);
        this.save(user);
    }

    // 根据ID获取用户信息
    @Override
    public List<ZjWorkUser> getByIds(List<Long> ids) {
        if (CollectionUtil.isEmpty(ids)) {
            return new ArrayList<>();
        }
        QueryWrapper<ZjWorkUser> wrapper = new QueryWrapper<>();
        wrapper.in("id", ids);
        return this.list(wrapper);
    }

    // 根据工单ID获取工单客户地址
    @Override
    public SelectAddressVo getWorkUserAddress(Long workId) {
        ZjWorkUser workUser = this.getById(workId);
        Assert.isTrue(null != workUser, "工单客户ID参数["+ workId +"]的工单客户数据不存在");
        SelectAddressVo vo = new SelectAddressVo();
        BeanUtil.copyProperties(workUser, vo);
        vo.setId(workUser.getUserId());
        return vo;
    }

    // 根据客户ID获取所有工单数量
    @Override
    public long getTotalOrderQtyByUserId(Long userId) {
        return workUserMapper.getTotalOrderQtyByUserId(userId);
    }

    // 根据客户ID获取工单信息
    @Override
    public List<ZjWorkUser> getByUserIds(List<Long> userIds) {
        if (CollectionUtil.isEmpty(userIds)) {
            return new ArrayList<>();
        }
        QueryWrapper<ZjWorkUser> wrapper = new QueryWrapper<>();
        wrapper.select("user_id");
        wrapper.in("user_id", userIds);
        return this.list(wrapper);
    }
}
